<?php

include('lib/src/config.php');

require_once "lib/src/apiClient.php";
require_once "lib/src/contrib/apiCalendarService.php";

//$calID='primary';
//$calID='tom@stanley5.com';
//$calID = '#contacts@group.v.calendar.google.com';
//$calID = 'en.usa#holiday@group.v.calendar.google.com';
$calID = 'stanley5.com_p13b36incoflgq8076i6lvqk0k@group.calendar.google.com';  //Family Tree

session_start();

//  AUTHENITCATE

$apiClient = new apiClient();
$apiClient->setUseObjects(true);
$service = new apiCalendarService($apiClient);

if (isset($_SESSION['oauth_access_token'])) {
  $apiClient->setAccessToken($_SESSION['oauth_access_token']);
} else {
  $token = $apiClient->authenticate();     //has to be failing here. This is where it all happens and the token is empty
  $_SESSION['oauth_access_token'] = $token;
}

?>

<html>

<body>
<p>
Here are the events in the calendar:
</p>

<pre>
<?php

//  RETRIEVE LIST OF EVENTS

$events = $service->events->listEvents($calID);  //retrieves first page of results

//  OUTPUT

while(true) {
  foreach ($events->getItems() as $event) {
    echo $event->id . "\n";
	print_r($event->start->date);
	print(" $event->summary \n");
//	print_r($event->getWhen());
  }
  $pageToken = $events->getNextPageToken();  //is there another page of results?
  if ($pageToken) {
    $optParams = array('pageToken' => $pageToken);
    $events = $service->events->listEvents($calID, $optParams);  //retrieve next page of results
  } else {
    break;  //no more pages--leave while loop
  }
}

?>
</pre>

<p>Details:</p>
<pre>

<?php
print_r($events);
?>

</pre>

</body>
</html>